/**
 * 
 */
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import beans.Ingredient;

/**
 * @author Administrateur
 *
 */
public class DAOIngredient extends DAO<Ingredient> {

	Connection laCo = null;
	PreparedStatement leStm = null;
	ResultSet result = null;
	
	@Override
	public Ingredient create(Ingredient obj) {
		try {
			laCo = dataSource.getConnection();
			
			leStm=laCo.prepareStatement("INSERT INTO ingredient (nom, prixachat, stockactuel) VALUES (?, ?, ?)");
			leStm.setString(1, obj.getNom());
			leStm.setFloat(2, obj.getPrix());
			leStm.setInt(3, obj.getQuantite());
			
			leStm.executeUpdate();
			
			leStm.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if(laCo != null) laCo.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return obj;
	}

	@Override
	public Ingredient update(Ingredient obj) {
		try {
			laCo = dataSource.getConnection();
			
			leStm=laCo.prepareStatement("UPDATE ingredient SET nom=?, prixachat=?, stockactuel=? WHERE id=?");
			leStm.setString(1, obj.getNom());
			leStm.setFloat(2, obj.getPrix());
			leStm.setInt(3, obj.getQuantite());
			leStm.setInt(4, obj.getId());
			
			leStm.executeUpdate();
			
			leStm.close();			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if(laCo != null) laCo.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return obj;
	}

	@Override
	public void delete(Ingredient obj) {
		try {
			laCo = dataSource.getConnection();
			
			leStm=laCo.prepareStatement("DELETE FROM ingredient WHERE id = ?");
			leStm.setInt(1, obj.getId());
			
			leStm.executeUpdate();
			
			leStm.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if(laCo != null) laCo.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public Ingredient find(long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Ingredient> findAll() {
		List<Ingredient> ret = null;
		try {
			laCo = dataSource.getConnection();
			
			leStm=laCo.prepareStatement("SELECT id, nom, prixachat, stockactuel FROM ingredient");
			
			result = leStm.executeQuery();
			
			ret = new ArrayList<Ingredient>();
			
			while(result.next()) {
				ret.add(new Ingredient(result.getInt(1),result.getString(2),result.getFloat(3),result.getInt(4)));
			}
			
			leStm.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if(laCo != null) laCo.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return ret;
	}

}
